/*
 * Copyright 2012 Mirella Esther da Silva
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mirella.alerts;

import java.io.InputStream;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.mirella.parser.Parser;
import com.mirella.parser.csv.CSVParser;
import com.mirella.parser.kml.KMLParser;

/**  
 *  Overlay general controller. 
 */

public class AlertOverlayLoader {

	private Context context;
	private List<Overlay> mapOverlays;

	private AlertOverlay fireAlerts;
	private AlertOverlay tornadoAlerts;
	private AlertOverlay hailAlerts;
	private AlertOverlay windAlerts;
	private AlertOverlay floodAlerts;

	public AlertOverlayLoader(Context context, List<Overlay> mapOverlays) {
		this.context = context;
		this.mapOverlays = mapOverlays;
	}
	
	public void loadAlerts() {
		this.getFireAlerts();
		this.getFloodAlerts();
		this.getWindAlerts();
		this.getHailAlerts();
		this.getTornadoAlerts();
	}
	
	public AlertOverlay getFireAlerts() {
		if(fireAlerts == null) {
			Log.i("AlertOverlayLoader", "Loading Fire alerts"); 
			Drawable drawable = context.getResources().getDrawable(R.drawable.ic_fire);
			fireAlerts = new AlertOverlay(drawable, context);

			KMLParser parser = new KMLParser();
			new DownloadAlertsTask().execute(fireAlerts, parser, "http://activefiremaps.fs.fed.us/data/kml/conus.kml");
		}
		return fireAlerts;
	}

	public AlertOverlay getTornadoAlerts() {
		if(tornadoAlerts == null) {
			Log.i("AlertOverlayLoader", "Loading Tornado alerts");
			Drawable drawable = context.getResources().getDrawable(R.drawable.ic_tornado);
			tornadoAlerts = new AlertOverlay(drawable, context);

			CSVParser parser = new CSVParser();
			new DownloadAlertsTask().execute(tornadoAlerts, parser, "http://www.spc.noaa.gov/climo/reports/today_torn.csv");
		}
		return tornadoAlerts;
	}

	public AlertOverlay getWindAlerts() {
		if(windAlerts == null) {
			Log.i("AlertOverlayLoader", "Loading Wind alerts");
			Drawable drawable = context.getResources().getDrawable(R.drawable.ic_wind);
			windAlerts = new AlertOverlay(drawable, context);

			CSVParser parser = new CSVParser();
			new DownloadAlertsTask().execute(windAlerts, parser, "http://www.spc.noaa.gov/climo/reports/today_wind.csv");
		}
		return windAlerts;
	}

	public AlertOverlay getHailAlerts() {
		if(hailAlerts == null) {
			Log.i("AlertOverlayLoader", "Loading Hail alerts");
			Drawable drawable = context.getResources().getDrawable(R.drawable.ic_hail);
			hailAlerts = new AlertOverlay(drawable, context);

			CSVParser parser = new CSVParser();
			new DownloadAlertsTask().execute(hailAlerts, parser, "http://www.spc.noaa.gov/climo/reports/today_hail.csv");
		}
		return hailAlerts;
	}


	public AlertOverlay getFloodAlerts() {
		if(floodAlerts == null) {
			Log.i("AlertOverlayLoader", "Loading Flood alerts");
			Drawable drawable = context.getResources().getDrawable(R.drawable.ic_flood);
			floodAlerts = new AlertOverlay(drawable, context);

			KMLParser parser = new KMLParser();
			new DownloadAlertsTask().execute(floodAlerts, parser, "http://www.hpc.ncep.noaa.gov/kml/fop/fopbody.kml");
		}
		return floodAlerts;
	}

	private class DownloadAlertsTask extends AsyncTask<Object, Void, Void> {
		@Override
		protected Void doInBackground(Object... urls) {
			AlertOverlay alertOverlay = (AlertOverlay)urls[0];
			Parser parser = (Parser)urls[1];
			String url = (String)urls[2]; 
			DefaultHttpClient client = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(url);
			try {
				HttpResponse execute = client.execute(httpGet);
				InputStream content = execute.getEntity().getContent();
				List<Alert> alerts = parser.parse(content); 
				for(Alert a : alerts) {
					OverlayItem overlayitem = new OverlayItem(new GeoPoint(a.getLat(), a.getLng()), a.getTitle(), a.getDescription()); 
					alertOverlay.addOverlay(overlayitem);
				}
				mapOverlays.add(alertOverlay);
			} catch (Exception e) {
				Log.e("AlertOverlayLoader", "error downloading files", e); 
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void v) {
		}
	}



}
